package cn.edu.xmu.oomall.customer.service;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.dao.ShoppingCartsDao;
import cn.edu.xmu.oomall.customer.microservice.CouponActivityService;
import cn.edu.xmu.oomall.customer.microservice.GoodsService;
import cn.edu.xmu.oomall.customer.microservice.vo.SimpleCouponActivityRetVo;
import cn.edu.xmu.oomall.customer.microservice.vo.ProductRetVo;
import cn.edu.xmu.oomall.customer.microservice.vo.SimpleCouponActivityVo;
import cn.edu.xmu.oomall.customer.microservice.vo.SimpleProductVo;
import cn.edu.xmu.oomall.customer.model.bo.Carts;
import cn.edu.xmu.oomall.customer.model.vo.CartsRetVo;
import cn.edu.xmu.oomall.customer.model.vo.CartsVo;
import cn.edu.xmu.oomall.customer.model.vo.SimpleCartRetVo;
import cn.edu.xmu.privilegegateway.annotation.util.InternalReturnObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

/**
 * @author Jiawei Zheng
 * @date 2021-12-04
 */
@Service
public class ShoppingCartsService {

    @Autowired
    private ShoppingCartsDao shoppingCartsDao;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CouponActivityService couponActivityService;

    /**
     * 买家获取购物车列表
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    public ReturnObject getCarts(Long userId, Integer page, Integer pageSize){
        ReturnObject<PageInfo<Carts>> returnObject = shoppingCartsDao.getCarts(userId, page, pageSize);
        if(returnObject.getData()==null){
            return returnObject;
        }
        List<Carts> cartsList = returnObject.getData().getList();
        List<CartsRetVo> cartsRetVoList = new ArrayList<>();
        for(Carts carts:cartsList){
            Long productId = carts.getProductId();
            CartsRetVo cartsRetVo = cloneVo(carts, CartsRetVo.class);

            //获取商品的详细信息
            InternalReturnObject<ProductRetVo> internalReturnObject = goodsService.getProductDetails(productId);
            if(internalReturnObject.getData()==null){
                return new ReturnObject(internalReturnObject);
            }
            SimpleProductVo simpleProductVo = cloneVo(internalReturnObject.getData(),SimpleProductVo.class);
            cartsRetVo.setProduct(simpleProductVo);
            //根据商品找优惠活动信息
            List<SimpleCouponActivityVo> simpleCouponActivityVoList = getCouponActivitiesOfProduct(productId);
            cartsRetVo.setCouponActivity(simpleCouponActivityVoList);
            cartsRetVoList.add(cartsRetVo);
        }
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(returnObject.getData().getPageNum());
        pageInfo.setPages(returnObject.getData().getPages());
        pageInfo.setPageSize(returnObject.getData().getPageSize());
        pageInfo.setTotal(returnObject.getData().getTotal());
        pageInfo.setList(cartsRetVoList);
        return new ReturnObject(pageInfo);
    }

    /**
     * 新增购物车
     * @param userId
     * @param userName
     * @param cartsVo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ReturnObject addCarts(Long userId, String userName, CartsVo cartsVo){

        Long productId = cartsVo.getProductId();
        //找商品的详细信息
        InternalReturnObject<ProductRetVo> internalReturnObject = goodsService.getProductDetails(productId);
        //找不到该商品，说明传入的productId有误
        if(internalReturnObject.getData()==null){
            return new ReturnObject(internalReturnObject);
        }
        ReturnObject returnObject = null;
        //获取商品价格price
        Carts carts = cloneVo(cartsVo, Carts.class);
        carts.setPrice(internalReturnObject.getData().getPrice());
        carts.setCustomerId(userId);
        ReturnObject<Carts> returnObject0 = shoppingCartsDao.selectCartsByProductId(userId, cartsVo.getProductId());
        if(returnObject0.getData()!=null){
            Carts bo = returnObject0.getData();
            if(carts.getQuantity()==null){
                carts.setQuantity(1L+bo.getQuantity());
            }
            else if(carts.getQuantity()<0&&Math.abs(carts.getQuantity())>=bo.getQuantity()){
                shoppingCartsDao.deleteCarts(userId, bo.getId());
                return new ReturnObject(new SimpleCartRetVo());
            }
            else{
                carts.setQuantity(carts.getQuantity()+bo.getQuantity());
            }
            carts.setId(bo.getId());
            shoppingCartsDao.updateCart(carts);
            return new ReturnObject(cloneVo(carts, SimpleCartRetVo.class));
        }
        else{
            //插入shopping_carts表
            returnObject = shoppingCartsDao.addCarts(userId, userName, carts);
            if(returnObject.getData()==null){
                return returnObject;
            }
            SimpleCartRetVo simpleCartRetVo = cloneVo(returnObject.getData(), SimpleCartRetVo.class);
            return new ReturnObject(simpleCartRetVo);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public ReturnObject clearCarts(Long userId){
        return shoppingCartsDao.clearCarts(userId);
    }

    @Transactional(rollbackFor = Exception.class)
    public ReturnObject updateCarts(Long userId, String userName, Long id, CartsVo cartsVo){
        Carts carts = cloneVo(cartsVo, Carts.class);
        return shoppingCartsDao.updateCarts(userId, userName, id, carts);
    }

    /***
     * 买家删除购物车中商品
     * @param id 购物车记录id
     * @return ReturnObject
     */
    @Transactional(rollbackFor = Exception.class)
    public ReturnObject deleteCarts(Long userId, Long id){

        return shoppingCartsDao.deleteCarts(userId, id);
    }

    private List<SimpleCouponActivityVo> getCouponActivitiesOfProduct(Long productId){
        //根据商品找优惠活动信息
        List<SimpleCouponActivityRetVo> simpleCouponActivityRetVoList = new ArrayList<>();
        List<SimpleCouponActivityVo> simpleCouponActivityVoList = new ArrayList<>();
        InternalReturnObject<PageInfo<SimpleCouponActivityRetVo>> internalReturnObject = couponActivityService.listCouponActivitiesByProductId(productId, 1, Integer.MAX_VALUE);
        if(internalReturnObject.getData()!=null){
            simpleCouponActivityRetVoList = internalReturnObject.getData().getList();
        }
        if(simpleCouponActivityRetVoList !=null){
            for(SimpleCouponActivityRetVo retVo: simpleCouponActivityRetVoList){
                SimpleCouponActivityVo simpleCouponActivityVo = cloneVo(retVo, SimpleCouponActivityVo.class);
                simpleCouponActivityVoList.add(simpleCouponActivityVo);
            }
        }
        return simpleCouponActivityVoList;
    }
}